#!/bin/bash

# update-lsvpd-db - Initialise lsvpd database.

# (C) Copyright IBM Corp. 2002, 2003, 2004, 2005

# This file is part of the Linux lsvpd package.

# Maintained by Martin Schwenke <martins@au.ibm.com>

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
    
# $Id: update-lsvpd-db.in,v 1.1 2006/04/11 18:38:28 emunson Exp $

if [ -z "$LSVPD_LIBDIR" ] ; then
    LSVPD_LIBDIR="/lib/lsvpd" ; export LSVPD_LIBDIR
fi

PATH="${LSVPD_LIBDIR}:/sbin:${PATH}:/usr/sbin:/usr/local/sbin" ; export PATH

. "${LSVPD_LIBDIR}/lsvpd-functions.bash"

shopt -s nullglob

######################################################################

usage () {
    cat <<EOF 1>&2
usage: $0 [options]
options: -h      print this usage message
         -d      Print debug messages to stderr
EOF
    exit 1
}

# Options handling
options=':'
options="${options}h"
options="${options}d"
while getopts ${options} opt
do
    case ${opt}
    in
	h ) usage
	    ;;
	d )
	    LSVPD_DEBUG=true
	    ;;
        \?) echo "unknown option \`${OPTARG}'" 1>&2
	    echo 1>&2
	    usage
            ;;
    esac
done
shift $((${OPTIND} - 1))

[ -n "$1" ] && usage

. "${LSVPD_LIBDIR}/debug.bash"

######################################################################

process_buses ()
{
    local sequence_number bus_list bus_info bus_type bus_node

    list_buses

    for bus_info in $bus_list ; do

	bus_type="${bus_info%%@*}"
	bus_node="${bus_info#*@}"

	add_bus "$bus_type" "$bus_node"

	set_sequence_number_hook "bus_${bus_type}"
	if [ -n "$sequence_number" ] ; then
	    local vpd_dir
	    vpd_dir_set_hook "$bus_node"
	    [ -e "$vpd_dir" ] && \
		vpd_field_ensure "$vpd_dir" "AX" "${bus_type}${sequence_number}"
	else
	    : failed to get sequence_number
	fi
    done
}

process_adapters ()
{
    local adapter_list
    list_adapters

    local bus_info
    for bus_info in $adapter_list ; do
	local bus_type="${bus_info%%/*}"
	local bus_addr="${bus_info#*/}"

	node_add "adapter" "$bus_type" "$bus_addr"
	node_add_name "adapter" "$bus_type" "$bus_addr"
    done
}

process_devices ()
{
    local device_list
    list_devices

    local bus_info
    for bus_info in $device_list ; do
	local bus_type="${bus_info%%/*}"
	local bus_addr="${bus_info#*/}"

	node_add "device" "$bus_type" "$bus_addr"
	node_add_name "device" "$bus_type" "$bus_addr"
    done
}

extras_handlers=""
process_extras ()
{
    local e
    for e in $extras_handlers ; do
	$e
    done
}

######################################################################

do_setup "scan"

datetime=$(date '+%Y-%m-%d-%H%M%S')
db_basename="${db_prefix}-${datetime}"
db="${vardir}/${db_basename}"
LSVPD_DB_DIR="$db" ; export LSVPD_DB_DIR

db_initialise

######################################################################

# Buses.
process_buses

# Adapters (before devices, since adapter matching should be done first).
process_adapters

# Devices.
process_devices

# Extras.
process_extras

######################################################################

db_link="${vardir}/${db_prefix}"

# If old directory instead of symlink, move it to a safe place.  This
# should not happen.
[ -d "$db_link" -a ! -L "$db_link" ] && \
    debug_cmd mv "$db_link" "${db_link}-0000-00-00-000000.$$"

if ! debug_cmd ln -snf "$db_basename" "$db_link" ; then
    echo "$0: failed to created updated database link \"$db_link\"" >&2
    exit 1
fi
